<template>
  <view class="mobile-item-container">
    <Navbar :title="id ? '修改' : '新增'" bgColor="#fff" :h5Show="false"></Navbar>
	<uni-section :title="id ? '修改' : '新增'" type="line">
		<view class="example">
			<uni-forms ref="form" :model="formData" :rules="rules" labelWidth="80px">
				<uni-forms-item label="司机名称" required name="userId">
					<uni-data-select v-model="formData.userId" :localdata="actions" >
					</uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="出车日期" required name="transportTime">
					<uni-datetime-picker type="date" return-type="timestamp"  v-model="formData.transportTime" />
				</uni-forms-item>
				
				<uni-forms-item label="起点" required name="startPoint">
					<uni-easyinput v-model="formData.startPoint" placeholder="请输入起点" />
				</uni-forms-item>
				
				<uni-forms-item label="终点" required name="endPoint">
					<uni-easyinput v-model="formData.endPoint" placeholder="请输入终点" />
				</uni-forms-item>
				
				<uni-forms-item label="公里数" required name="kilometer">
					<uni-easyinput v-model="formData.kilometer" placeholder="请输入公里数" />
				</uni-forms-item>
				
				<uni-forms-item label="运费" required name="freight">
					<uni-easyinput v-model="formData.freight" placeholder="请输入运费" />
				</uni-forms-item>
				<uni-forms-item label="状态" required>
					<uni-data-checkbox v-model="formData.status" :localdata="status" />
				</uni-forms-item>
				<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="formData.remark" placeholder="请输入备注" />
				</uni-forms-item>
			</uni-forms>
			<view class="button-group">
				<u-row :gutter="12">
				  <u-col :span="4">
				    <u-button v-if="id" type="error" text="删除" @click="del"  v-hasPermi="['transportInfo:transportInfo:remove']"></u-button>
				  </u-col>
				  <u-col :span="4">
				    <u-button icon="arrow-left" text="返回" plain @click="goBack()"></u-button>
				  </u-col>
				  <u-col :span="4">
					  <u-button v-if="!id" type="primary" text="新增" @click="submit" v-hasPermi="['transportInfo:transportInfo:add']"></u-button>
					  <u-button v-else  type="primary" text="修改" @click="submit" v-hasPermi="['transportInfo:transportInfo:edit']"></u-button>
				  </u-col>
				</u-row>
			</view>
		</view>
	</uni-section>
	
	<buttom-tabbar></buttom-tabbar>
  </view>
</template>

<script>
import * as TransportInfoApi from '@/api/transportInfo/transportInfo'
import * as DictInfoApi from '@/api/system/dict/data'
import * as UserInfoApi from '@/api/user'
import Navbar from '@/components/navbar/Navbar'
import { selectUserDictLabel } from '../../../utils/ruoyi'

export default {
  components: {
    Navbar,
  },
  data () {
    return {
      id: undefined,
      formData:{
              id: null,
              userId: null,
			  nickName:null,
              transportTime: null,
              startPoint: null,
              endPoint: null,
              kilometer: null,
              freight: null,
              status: '1',
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              remark: null
        },
	  actions: [],
	  actionShow: false,
	  nickName:"",
	  actionTitle: '',
	  // 校验规则
	  rules: {
	  	userId: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '司机名称不能为空'
	  		}]
	  	},
	  	transportTime: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '出车日期不能为空'
	  		}]
	  	},
	  	startPoint: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '起点不能为空'
	  		}]
	  	},
	  	endPoint: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '终点不能为空'
	  		}]
	  	},
	  	kilometer: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '公里数不能为空'
	  		}, {
	  			format: 'number',
	  			errorMessage: '公里数只能输入数字'
	  		}]
	  	},
	  	freight: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '运费不能为空'
	  		}, {
	  			format: 'number',
	  			errorMessage: '运费只能输入数字'
	  		}]
	  	}
	  },
	  showCalendar:false,
	  // 单选数据源
	  status: [],
    }
  },

  onLoad (params) {
	this.selectUserListByPostCode();
    this.id = params.id
    this.loadData()
	DictInfoApi.getDicts("transport_info_status").then(res => {
		if(res.data)
		{
			if(res.data.length)
			{
				this.status=[]
				for(let i=0;i<res.data.length;i++)
				{
					let temp = res.data[i];
					let obj = {};
					obj.value = temp.dictValue;
					obj.text = temp.dictLabel
					this.status.push(obj);
				}
			}
		}
	});
  },
  methods: {
    loadData () {
      if (this.id) {
        const app = this
        TransportInfoApi.getTransportInfo(this.id).then(res => {
			if(res.data)
			{
				app.formData = res.data
				this.nickName = selectUserDictLabel('userId','nickName',app.formData.userId)
				app.formData.nickName = this.nickName
			}
        })
      }else
	  {
		  this.formData.userId =  parseInt(this.$store.getters.userId)
	  }
    },
	selectUserListByPostCode()
	{
	  const postCode = 'sj';
	  UserInfoApi.selectUserListByPostCode(postCode).then((response) => {
		if(response.rows)
		{
			if(response.rows.length)
			{
				for(let i=0;i<response.rows.length;i++)
				{
					let temp = response.rows[i];
					let obj = {};
					obj.value = temp.userId;
					obj.text = temp.nickName
					this.actions.push(obj);
				}
			}
		}
	  });
	},
    del() {
		let id = this.id
		uni.showModal({
			title: '提示',
			content: '确定要删除吗？',
			success: function (res) {
				if (res.confirm) {
					TransportInfoApi.delTransportInfo(id).then(res2 => {
					  uni.showToast({ title: '提交成功！' })
					  uni.navigateTo({ url: '/pages/transportInfo/transportInfo/index'})
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
    },
    submit() {
		this.$refs.form.validate().then(res => {
		  if (this.id) {
		    TransportInfoApi.updateTransportInfo(this.formData).then(res => {
		      uni.showToast({ title: '提交成功！' })
					this.goBack()
		    })
		  } else {
		    TransportInfoApi.addTransportInfo(this.formData).then(res => {
		      uni.showToast({ title: '提交成功！' })
					this.goBack()
		    })
		  }
		});
    },
    actionSelect (item) {
      this.nickName = item.name;
      this.formData.userId = item.value;
	  this.formData.nickName = item.name;
      this.$refs.form.validateField('nickName');
    },
    goBack() {
		uni.navigateTo({ url: '/pages/transportInfo/transportInfo/index'})
    },
	hideKeyboard() {
		uni.hideKeyboard()
	},
	calendarConfirm(e) {
		this.showCalendar = false
		this.formData.transportTime = e[0]
		this.$refs.form.validateField('transportTime')
	},

  }
}
</script>
<style lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>
